import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

public class MyTimer {
   //创建阻塞队列来组织任务
    private BlockingQueue<MyTask> blockingQueue = new PriorityBlockingQueue<>();

    //创建线程扫描队列  放在构造方法中
    public MyTimer(){
        Thread thread = new Thread(()->{
           while(true) {
               try {
                   MyTask task = blockingQueue.take();
                   
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
    }

    /**
     * 创建装填任务的方法
     */
    public void schedule(Runnable runnable,long time) throws InterruptedException {
        MyTask myTask = new MyTask(runnable,time);
        blockingQueue.put(myTask);
    }
}
//创建装填任务与时间的容器
class MyTask implements Comparable<MyTask>{
    private Runnable runnable;
    private long time;

    public MyTask(Runnable runnable, long time) {
        if(runnable == null) {
            throw new IllegalArgumentException("任务不能为空");
        }
        if(time < 0) {
            throw new IllegalArgumentException("时间不能小于0");
        }
        this.runnable=runnable;
        this.time=time-System.currentTimeMillis();
    }

    public Runnable getRunnable() {
        return runnable;
    }

    public long getTime() {
        return time;
    }

    @Override
    public int compareTo(MyTask o) {
        if(o.getTime() > this.getTime()) {
            return -1;
        }else if(o.getTime() < this.getTime()){
            return 1;
        }else {
            return 0;
        }
        //return (int)(this.getTime()-o.getTime())
    }
}
